package com.bdcom.autotest.interaction.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.bdcom.autotest.ui.CenterPanel;
import com.bdcom.autotest.util.Prop;

public class PmgrDB {
	private static Logger log = LoggerFactory.getLogger(PmgrDB.class);
	
	private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private static final String IP = Prop.devConfig.getProperty("pmgrIp");
//	private static final String IP = "172.16.2.5";
    private static final String URL = "jdbc:sqlserver://"+IP+":1433;databasename=";
    private static final String DATABASE_NAME = "pmgr";
    private static final String USER_NAME = "sa";
    private static final String PASSWORD = "laoni";
    
    
    public static final String table_order = "assemblyworkorder"; //                  装配工单
    public static final String table_sub_order = "assemblysubworkorder"; //                 装配工单明细/子工单
    
    
    public static Connection getConn() {
	   try{
           Class.forName( DRIVER );           
           Connection conn = DriverManager.getConnection(URL+DATABASE_NAME,USER_NAME,PASSWORD);
           return conn;
       }catch(Exception e){
    	   log.error( "" ,e );
       } 
	   return null;	
    }
    
    public static void closeRes( Statement stmt , ResultSet rs ,Connection conn   ) {
    	if(  stmt !=null ) {
    		try {
				stmt.close();
			} catch (SQLException e) {
				log.error( "" ,e );
			}
    	}
      	if(  rs !=null ) {
    		try {
    			rs.close();
			} catch (SQLException e) {
				log.error( "" ,e );
			}
    	}
     	if(  conn !=null ) {
    		try {
    			conn.close();
			} catch (SQLException e) {
				log.error( "" ,e );
			}
    	}
    }
    
    public static void closeRes( Connection conn   ) {
     	if(  conn !=null ) {
    		try {
    			conn.close();
			} catch (SQLException e) {
				log.error( "" ,e );
			}
    	}
    }
    
    public static void closeRes( Statement stmt , ResultSet rs ) {
    	if(  stmt !=null ) {
    		try {
				stmt.close();
			} catch (SQLException e) {
				log.error( "" ,e );
			}
    	}
      	if(  rs !=null ) {
    		try {
    			rs.close();
			} catch (SQLException e) {
				log.error( "" ,e );
			}
    	}
    }
    
    
    public static void main(String args[]){
        try{
            Class.forName( DRIVER );           
            Connection conn = DriverManager.getConnection(URL+DATABASE_NAME,USER_NAME,PASSWORD);
            //新建一个查询
            Statement stmt = conn.createStatement();
            
            //执行查询-->>返回一个结果集
            ResultSet rs = stmt.executeQuery( "select count(Id) from " + table_order );    //括号里可以写相关的SQL语句，并把查询到的所有，放到一个rs集合里
            boolean next = rs.next();
            Object object = rs.getObject(1);
            System.out.println(   "记录总数：" +  object );
            rs.close();
            stmt.close();
            conn.close();
            
        }catch(Exception e){
            e.printStackTrace();
        } 
    }
    
    
}
